/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.GoodsModel;
import com.hykj.xxltest.service.Class2Service;
import com.hykj.xxltest.service.GoodsService;
import com.hykj.xxltest.utils.InnelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author innel
 * @date 2018-06-26 15:50:31
 */
@Controller
@RequestMapping("/goods")
@Api(description = "商品")
public class GoodsController {

    private static final Logger logger = Logger.getLogger(GoodsController.class);

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private Class2Service class2Service;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            response = GoodsModel.class,
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "1保税区 2大贸商品")
                        @RequestParam(required = false)
                                Integer goodsType,
                        @ApiParam(value = "0正常 -1下架 ")
                        @RequestParam(required = false)
                                Integer status,
                        @ApiParam(value = "总销量")
                        @RequestParam(required = false)
                                Integer saleNum,
                        @ApiParam(value = "库存")
                        @RequestParam(required = false)
                                Integer kucun,
                        @ApiParam(value = "参与活动的ID")
                        @RequestParam(required = false)
                                Integer eventId,
                        @ApiParam(value = "所属分类")
                        @RequestParam(required = false)
                                Integer classId,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("goodsType", goodsType);
        params.put("status", status);
        params.put("saleNum", saleNum);
        params.put("kucun", kucun);
        params.put("eventId", eventId);
        params.put("classId", classId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(goodsService.getDList(params));
        }
        PageInfo<GoodsModel> p = goodsService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @ApiOperation(
            value = "获取",
            notes = "备注：",
            response = GoodsModel.class,
            httpMethod = "POST")
    public AppBack info(
            @ApiParam(value = "的ID", required = true)
            @RequestParam Integer goodsId) {
        GoodsModel goods = goodsService.getByIdD(goodsId);
        if (goods == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(
            @ApiParam(value = "标题", required = true)
            @RequestParam
                    String title,

//            @ApiParam(value = "小标题", required = false)
//            @RequestParam
                    String title2,

//            @ApiParam(value = "1保税区 2大贸商品", required = true)
//            @RequestParam
                    Integer goodsType,

//            @ApiParam(value = "sku", required = true)
//            @RequestParam
                    String sku,

//            @ApiParam(value = "标签 例如正品保障，海外直供", required = true)
//            @RequestParam
                    String tag,

//            @ApiParam(value = "税费", required = true)
//            @RequestParam
                    BigDecimal taxPrice,
            @ApiParam(value = "原价", required = true)
            @RequestParam
                    BigDecimal originalPrice,

            @ApiParam(value = "0正常 -1下架 ", required = true)
            @RequestParam
                    Integer status,

            @ApiParam(value = "商品详情 富文本", required = true)
            @RequestParam
                    String detail,

            @ApiParam(value = "内容轮播图 ,分割", required = true)
            @RequestParam
                    String contentPhoto,

            @ApiParam(value = "总销量", required = true)
            @RequestParam
                    Integer saleNum,

            @ApiParam(value = "封面图", required = true)
            @RequestParam
                    String facePhoto,

            @ApiParam(value = "库存", required = true)
            @RequestParam
                    Integer kucun,

//            @ApiParam(value = "商品规矩json串 例如 [{name:\"品牌\",value:\"雕牌\"}]", required = false)
//            @RequestParam
                    String attrJson,
                    BigDecimal price) {
        GoodsModel goods = new GoodsModel();

        goods.setTitle(title);


        goods.setTitle2(title2);


        goods.setGoodsType(goodsType);


        goods.setSku(sku);


        goods.setTag(tag);


        goods.setTaxPrice(taxPrice);


        goods.setOriginalPrice(originalPrice);


        goods.setStatus(status);


        goods.setDetail(detail);


        goods.setContentPhoto(contentPhoto);


        goods.setSaleNum(saleNum);


        goods.setFacePhoto(facePhoto);


        goods.setKucun(kucun);


        goods.setAttrJson(attrJson);
        goods.setPrice(price);

        goodsService.create(goods);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "标题")
                          @RequestParam(required = false)
                                  String title,
                          @ApiParam(value = "小标题")
                          @RequestParam(required = false)
                                  String title2,
                          @ApiParam(value = "1保税区 2大贸商品")
                          @RequestParam(required = false)
                                  Integer goodsType,
                          @ApiParam(value = "sku")
                          @RequestParam(required = false)
                                  String sku,
                          @ApiParam(value = "标签 例如正品保障，海外直供")
                          @RequestParam(required = false)
                                  String tag,
                          @ApiParam(value = "税费")
                          @RequestParam(required = false)
                                  BigDecimal taxPrice,
                          @ApiParam(value = "原价")
                          @RequestParam(required = false)
                                  BigDecimal originalPrice,
                          @ApiParam(value = "0正常 -1下架 ")
                          @RequestParam(required = false)
                                  Integer status,
                          @ApiParam(value = "商品详情 富文本")
                          @RequestParam(required = false)
                                  String detail,
                          @ApiParam(value = "内容轮播图 ,分割")
                          @RequestParam(required = false)
                                  String contentPhoto,
                          @ApiParam(value = "总销量")
                          @RequestParam(required = false)
                                  Integer saleNum,
                          @ApiParam(value = "封面图")
                          @RequestParam(required = false)
                                  String facePhoto,
                          @ApiParam(value = "库存")
                          @RequestParam(required = false)
                                  Integer kucun,
                          @ApiParam(value = "商品规矩json串 例如 [{name:\"品牌\",value:\"雕牌\"}]")
                          @RequestParam(required = false)
                                  String attrJson,
                          @ApiParam(value = "参与活动的ID")
                          @RequestParam(required = false)
                                  Integer eventId,
                          @ApiParam(value = "商品的ID", required = true)
                          @RequestParam int goodsId,
                          @ApiParam(value = "商品价格", required = true)
                              @RequestParam BigDecimal price) {
        GoodsModel goods = new GoodsModel();
        goods.setTitle(title);
        goods.setTitle2(title2);
        goods.setGoodsType(goodsType);
        goods.setSku(sku);
        goods.setTag(tag);
        goods.setTaxPrice(taxPrice);
        goods.setOriginalPrice(originalPrice);
        goods.setStatus(status);
        goods.setDetail(detail);
        goods.setContentPhoto(contentPhoto);
        goods.setSaleNum(saleNum);
        goods.setFacePhoto(facePhoto);
        goods.setKucun(kucun);
        goods.setAttrJson(attrJson);
        goods.setEventId(eventId);
        goods.setId(goodsId);
        goods.setPrice(price);
        goodsService.update(goods);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "的ID", required = true)
                          @RequestParam
                                  int goodsId) {

        GoodsModel newGoods = new GoodsModel();
        newGoods.setStatus(-10);
        newGoods.setId(goodsId);
        goodsService.update(newGoods);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/addClass")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "添加分类",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack addClass(@ApiParam(value = "的ID", required = true)
                            @RequestParam
                                    String goodsIds,
                            int classId) {
        int[] goodsIda = InnelUtil.parseStringToInt(goodsIds);
        for (int goodsId : goodsIda) {
            GoodsModel goodsModel = goodsService.getById(goodsId);
            List<Integer> goodsIdList = InnelUtil.parseStirngToIntList(goodsModel.getClassId());
            if (goodsIdList.contains(classId)) {
                //添加分类重复
                continue;
            }
            goodsIdList.add(classId);
            String classIds = InnelUtil.parseIntListToString(goodsIdList);
            GoodsModel newGoods = new GoodsModel();
            newGoods.setId(goodsId);
            newGoods.setClassId(classIds);
            newGoods.setClassName(class2Service.getNameByIdList(goodsIdList));
            goodsService.update(newGoods);
            logger.info("添加分类 " + goodsIdList.toString());
        }

        return new AppBack();
    }


    /**
     * 删除
     */
    @RequestMapping("/deleteClass")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除分类",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack deleteClass(@ApiParam(value = "商品的ID", required = true)
                               @RequestParam
                                       int goodsId,
                               int classId) {
        GoodsModel goodsModel = goodsService.getById(goodsId);
        List<Integer> goodsIdList = InnelUtil.parseStirngToIntList(goodsModel.getClassId());
        goodsIdList.remove(new Integer(classId));
        String classIds = InnelUtil.parseIntListToString(goodsIdList);

        GoodsModel newGoods = new GoodsModel();
        newGoods.setId(goodsId);
        newGoods.setClassName(class2Service.getNameByIdList(goodsIdList));
        newGoods.setClassId(classIds);
        goodsService.update(newGoods);

        return new AppBack();
    }

    @RequestMapping("/html")
    @ResponseBody
    @ApiOperation(
            value = "获取商品详情网页链接",
            notes = "备注：",
            httpMethod = "GET")
    public String html(@ApiParam(value = "商品的ID", required = true)
                       @RequestParam
                               int id) {
        GoodsModel goodsModel = goodsService.getById(id);
        String res = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "    <style>\n" +
                "         img{\n" +
                "            width: 100% !important;\n" +
                "             height: auto;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body >\n" +
                goodsModel.getDetail() +
                "</body>\n" +
                "</html>";

        return res;
    }

}
